using System;

namespace KodeIT.Web
{
    /// <summary>
    /// This class represents the event raised by IIS when access is denied. If your HttpFilter should 
    /// be notified for this event, it should subscribe for the <see cref="IFilterEvents.AccessDenied"/> event.
    /// </summary>
    /// <remarks>This event indicates that the server has denied access to the requested resource. The 
    /// structure is generated when there has been a logon failure, or if a user requests a resource 
    /// that has an associated discretionary access control list that does not include the logged-on user. 
    /// The server will automatically include the supported authentication schemes when an ISAPI extension, 
    /// filter, or CGI script returns a 401 Access Denied error code.</remarks>
    public class AccessDeniedEvent : ContextEvent
    {
        private NativeString _mappedUrl;
        private NativeString _mappedPath;
        private AccessDeniedReason _reason;

        internal AccessDeniedEvent(NativeContext nativeContext, int urlLength, int pathLength, AccessDeniedReason reason)
            : base(nativeContext)
        {
            NativeGetter getter = new NativeGetter(Populate);
            _mappedUrl = new NativeString(urlLength, getter);
            _mappedPath = new NativeString(pathLength, getter);
            _reason = reason;
        }

        /// <summary>
        /// Gets the url that requested access to the resource.
        /// </summary>
        public string MappedUrl
        {
            get { return _mappedUrl.Data; }
        }

        /// <summary>
        /// Gets the physical path of the resource that was requested.
        /// </summary>
        public string MappedPath
        {
            get { return _mappedPath.Data; }
        }

        /// <summary>
        /// Gets the reason why the access was denied.
        /// </summary>
        public AccessDeniedReason Reason
        {
            get { return _reason; }
        }

        private void Populate()
        {
            NativeMethods.GetAccessDenied(NativeContext, _mappedUrl, _mappedPath);
        }
    }
}
